<?php
/*
 * ---------------------------------------------------------------
* Author  : Anthony Tran
* Email   : Incredibletran@gmail.com - Incredibletran@hotmail.com
* Version : 1.2
* ---------------------------------------------------------------
*/
class Video_model extends CI_Model{
	
	var $table	=	"videos";
	public function __construct(){
		parent::__construct();
	}
	
	
	function get_all($status = "all", $keyword = "", $date = ""){
		$this->db->select();
		if($status != "all"){
			$this->db->where('active', $status);
		}
		if($keyword != ""){
			$this->db->like('name', $keyword);
		}
		if($date != ""){
			$this->db->like('date_create', $date);
		}
		$this->db->order_by('id', 'DESC');
		$query	=	$this->db->get($this->table);
		$slide	=	$query->result();
		if(!empty($slide)){
			return $slide;
		}
		return false;
	}
	
	
	function get_list($perpage = "all", $offset = 0, $status = "all", $keyword = "", $date = ""){
		$this->db->select();
		if($status != "all"){
			$this->db->where('active', $status);
		}
		if($keyword != ""){
			$this->db->like('name', $keyword);
		}
		if($date != ""){
			$this->db->where('date_create', $date);
		}
		if($perpage != "all"){
			$this->db->limit($perpage, $offset);
		}
		$this->db->order_by('id', 'DESC');
		$query	=	$this->db->get($this->table);
		$result	=	$query->result();
		if(!empty($result)){
			return $result;
		}
		return false;
	}
	
	
	function add($video	=	array()){
		$res	=	$this->db->insert($this->table, $video);
		return $res;
	}
	
	function edit($id = 0, $info = array()){
		$this->db->where('id', $id)->update($this->table, $info);
	}
	
	function count_active(){
		return $this->db->where('active', 'yes')->count_all_results($this->table);
	}
	
	function count_all(){
		return $this->db->count_all($this->table);
	}
	
	
	function change_status($slider_id, $info = array()){
		return $this->db->where('id', $slider_id)
						->update($this->table, $info);
	}
	
	
	function get_match($id = 0){
		$res	=	$this->db->select()
							 ->where('id', $id)
							 ->get($this->table)
							 ->row();
		if(!empty($res)){
			return $res;
		}
		return false;
	
	}
	
	function get_match_order($order){
		$res	=	$this->db->where('order', $order)
							 ->get($this->table)
							 ->row_array();
		if(count($res)>0){
			return $res;
		}
		return $res;
	}
	
	
	function update_order($id, $info = array()){
		return $this->db->where('id', $id)->update($this->table, $info);
	}
	
	function update_many($id_list, $info = array()){
		return $this->db->where_in('id', $id_list)
						 ->set($info)
						 ->update($this->table);
	}
	
	
	function delete_many($id_list){
		return $this->db->where_in('id', $id_list)
						->delete($this->table);
	}
	
	function delete($video_id){
		return $this->db->where('id', $video_id)->delete($this->table);
	}
	
	
 	function check_exist($name = ""){
    	$res	=	$this->db->select()
					    	 ->where('name', $name)
					    	 ->get($this->table)
					    	 ->row_array();
    	if(!empty($res)){
    		return true;
    	}
    	return false;
    }
    
    function check_order_exist($order = 0){
    	$res	=	$this->db->select()
					    	 ->where('order', $order)
					    	 ->get($this->table)
					    	 ->row_array();
    	if(!empty($res)){
    		return true;
    	}
    	return false;
    }
    
    
    function check_exist_edit($id, $name){
    	$res	=	$this->db->select()
					    	 ->where('id !=', $id)
					    	 ->where('name', $name)
					    	 ->get($this->table)
					    	 ->row_array();
    	if(!empty($res)){
    		return true;
    	}
    	return false;
    }
    
    function check_order_exist_edit($id, $order){
    	$res	=	$this->db->select()
					    	 ->where('id !=', $id)
					    	 ->where('order', $order)
					    	 ->get($this->table)
					    	 ->row_array();
    	if(!empty($res)){
    		return true;
    	}
    	return false;
    }
    
    
    
	
}
/*---------------------------------------------End-----------------------------------------*/